package latin.controller.portal;

import latin.common.Const;
import latin.common.ServiceResponse;
import latin.pojo.User;
import latin.service.IUserService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/")
public class UserController {
    @Setter
    private String username;
    @Setter
    private String password;
    @Autowired
    private IUserService iUserService;

    //用户注册
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> register(User user){
        return iUserService.register(user);
    }
    //用户登录
    @RequestMapping(value ="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse login(String username,String password,HttpSession session){
        ServiceResponse<User> response = iUserService.login(username,password);
        //如果登录成功就把用户信息传入session中
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }
    //用户退出登录,只用把存储在session中的用户信息删除
    @RequestMapping(value = "logout.do")
    @ResponseBody
    public ServiceResponse logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServiceResponse.createBySuccess();
    }

    //获取忘记密码的问题
    @RequestMapping(value = "get_question.do")
    @ResponseBody
    public ServiceResponse<String> selectQuestion(String username){
        return iUserService.selectQuestion(username);
    }
    //获取answer
    @RequestMapping("get_answer.do")
    @ResponseBody
    public ServiceResponse<String> getAnswer(String username,String question,String answer){
        return iUserService.getAnswer(username,question,answer);
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //忘记密码
    @RequestMapping( "forget_reset_password.do")
    @ResponseBody
    public ServiceResponse forgetResetPassword(String username,String password,String forgetToken){
        return iUserService.forgetResetPassword(username,password,forgetToken);
    }
    //修改密码
    @RequestMapping("reset_password.do")
    @ResponseBody
    public ServiceResponse<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return  ServiceResponse.createByErrorMessage("用户未登录");
        }
        return  iUserService.resetPassword(passwordOld,passwordNew,user);
    }
    @RequestMapping("getUserInfo.do")
    @ResponseBody
    public ServiceResponse<User> getUserInfo(HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return  ServiceResponse.createByErrorMessage("用户未登录");
        }

        return ServiceResponse.createBySuccess(user);
    }
}
